ITI 1120 Winter 2013 - Assignment 2

Available: Feb 5, 2013
Due: Friday Feb 15, extended till Feb 17, 22:00

Instructions

This assignment is to be done INDIVIDUALLY. Question 1 should be answered in a Word file A2Q1.doc. Question 2 requires algorithm description and trace in A2Q2.doc and Java code in A2Q2.java (include A2Q2.class too as proof that it compiled). Question 3 requires you to implement a Java program in a file A2Q3.java and compile it in A2Q3.class. Zip all the .doc, .java, and .class files in a2_xxxxxx.zip, where xxxxxx is your student number, and submit it through the Blackboard Learn).

Your algorithms should be developed using the format used in class. Your algorithm traces should use the format shown in class; a separate table is to be used for each call to each algorithm.

Marking Scheme (total 100 marks)

Question 1 (10 + 10 marks)

A triangle has three angles C1, C2, and C3 (measured in degrees). Write a Boolean expression that is TRUE only if the triangle is isosceles but not equilateral.  (See types of triangles)

Question 1a) Write the Boolean expression using the algorithm format from class.  Keep in mind the difference between a Boolean expression, and a test used in a branch or loop.  You need only to write the expression (example:  A (B + 3) ), and not a branch structure or a whole algorithm body and its header. The Boolean expressions that you write for this question should use only the comparison operators (<, >, =, etc.), Boolean operators (AND, OR, NOT), and math operators +, , ×, /, or MOD.  Use parentheses where necessary. Do not use Java syntax!

Question 1b) Write the same Boolean expression using Java format.  Keep in mind the difference between a Boolean expression, and a test used in an if statement.  You need only to write the expression (example:  A <= (B + 3) ), and not the complete if statement (you do not need to submit a Java program for this question). The Java Boolean expressions that you write for this question should use only the comparison operators (<, >, ==, etc.), logical operators (&&, ||, !), and math operators +, , *, /, or % (modulo). Use parentheses where necessary.

 

Question 2 (15 + 10 + 15 marks) Algorithm + trace + Java code

Question 2a) Write an algorithm that determines the type of a triangle, if the lengths of the three sides are given. We are interested in four types of triangles (mutually exclusive):

- "Equilateral": the three sides of the triangle have the same length

- "Isocel": two sides of the triangle are equal, but the third one has a different length

- "Other": the three sides of the triangle have different lengths

- "Invalid": the three sides do not form a triangle.

 

For example, if we have as lengths of the sides 3, 5, and 3, the algorithm should return "Isocel". If we have 2, 5, and 3, the algorithm should return "Invalid". Assume that the three values input by the user are integers between 1 and 10000 inclusively.

 

Note: Three lengths do not form a triangle if the sum of the two smallest sides are less than or equal to the longest side. (You can imagine this by thinking of 3 sticks. If the 2 smaller ones are attached to the end of the longest one and they don't meet, it cannot form a triangle). Also see types of triangles for more details.

 

Question 2b) Trace your algorithm for the input values 3, 6, 5.

 

Question 2c) Java implementation

Write a simple Java program that implements the algorithm from Question 2a). Use the template seen in class as a starting point (make sure that the program prints the assignment and question number, and your name and student number). Use the ITI1120 class to read the three inputs in Java (display appropriate instructions to the user). The result will be printed on the display, together with an appropriate message. Make sure you copy the file ITI1120.class in your current directory. Alternatively, you can read input using the Scanner class. Test your program by yourself.

 

Question 3 (30 marks) - Java program

The game "Yo Jo" is played in the following manner:


Everybody sits in a circle and somebody starts the game by saying "One".  The next person says "Two", the next person says “Three”, and so on, continuing with the next persons in the circle. But ...

a) If the number is divisible by 9, or if it contains 9 as one of its digits, than you have to say "Yo!" instead of the number.

b) If the number is divisible by 5, or if it contains 5 as one of its digits, than you have to say "Jo!" instead of the number.

c) If both a) and b) apply, you have to say "Yo Jo!" instead of the number.

If you say the number when you should have said "Yo", "Jo", or "Yo Jo", or if you do not say the number when you should have,  you will be excluded from the game (or you get a "punishment"). The game restarts with the next person, with the next number. For example, if somebody said "Nine" instead of "Yo", the next person starts with "Ten" (or rather "Jo", in order to stay in the game).

Write a Java program that starts by asking the user to input a number between 1 and 99, inclusively, and that displays on the screen:

Do not submit the algorithm, but it is recommended to write the algorithm and to trace it before starting programming it in Java. You should also test your program, for yourself. You can assume, for simplicity, that the user inputs a valid integer value.

 

Have fun!